package com.orangelife.mobile.address.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.curry.android.http.HttpUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.address.adapter.AddressListAdapter;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AddressListActivity extends OrangeLifeBaseActivity {

    private LinearLayout addressManage;
    private TextView title;
    private ListView addresslist;
    private List<Map<String, Object>> listMap;
    private static final int REQUEST_CODE = 1;
    private AddressListAdapter<Map<String, Object>> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        initView();
        findView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        closeActivity();
    }

    private void initView() {
        addressManage = (LinearLayout) findViewById(R.id.ll_addressManage);
        title = (TextView) findViewById(R.id.tv_title);
        addresslist = (ListView) findViewById(R.id.lv_address);
    }

    /**
     * 设置适配器
     */
    private void setAdapter() {
        listMap = new ArrayList<>();
        adapter = new AddressListAdapter<>(
                this, listMap);
        addresslist.setAdapter(adapter);
        addresslist.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int position, long arg3) {
                dialog(listMap.get(position).get("addressID").toString());
                return true;
            }
        });
    }

    private void findView() {
        addressManage.setVisibility(View.INVISIBLE);
        title.setText("收货地址");
        setAdapter();
        addresslist.setOnItemClickListener(itemClickLintener);
        getAddressList();
    }

    private void getAddressList() {
        showDialog(this);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.ADDRESS_QUERY_URL, null, handler, 0, HttpUtil.ACTION_URI);
    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            closeDialog();
            switch (msg.what) {
                case 500:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    break;
                case 1001:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    isLogin(dialog);
                    break;
                case 0:
                    listMap = JsonReponseHandler.getListFromJson(msg.obj.toString(), adapter, dialog);
                    break;
                case 3:
                    getAddressList();
                    ToastHelper.getInstance()._toast("删除成功");
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }

    };


    AdapterView.OnItemClickListener itemClickLintener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> aview, View view, int position,
                                long arg3) {
            Intent intent = new Intent();
            intent.setClass(AddressListActivity.this, EditAddressActivity.class);
            Map<String, Object> map = listMap.get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("listmap", (Serializable) map);
            intent.putExtras(bundle);
            startActivityForResult(intent, REQUEST_CODE);
        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                getAddressList();
                break;
            default:
                break;
        }
    }

    public void addAddress(View view) {
        Intent intent = new Intent();
        intent.setClass(AddressListActivity.this, AddAddressActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    private void delAddress(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("{addressID}", id);
        JSONRequest.getInstance().sendRequest(JSONRequest.DELETE, Constant.ADDRESS_DELETE_URL, map, handler, 3, HttpUtil.ACTION_URI);
    }

    protected void dialog(final String id) {
        final ConfirmDialog dialog = new ConfirmDialog(AddressListActivity.this, "温馨提示", "是否删除？", "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                delAddress(id);
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    protected void dialog1(final String id) {
        AlertDialog.Builder builder = new Builder(AddressListActivity.this);
        builder.setMessage("是否删除？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                delAddress(id);

            }
        });
        builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }

        });
        builder.create().show();
    }
}
